<?php
include_once('./config/config.php');

// 判断有没有表单提交
if ($_POST) {
  $action = $_POST['action'];
  
  // 判断员工名称是否重复
  if ($action == 'name') {
    $name = $_POST['name'];
    $personRes = findOne("select * from person where name = '$name'");
    if ($personRes) {
      error("员工名称已存在");
    } else {
      success("员工名称可以使用");
    }
  }
  if ($action == 'depid') {
    $depid = $_POST['depid'];
    $data = findAll("select * from job where depid = '$depid'");
    // var_dump($depid);
    // var_dump($data);
    // exit;
    if ($data) {
      success("查询成功", $data);
    } else {
      error("暂无数据");
    }
  }
  // 员工名称可以使用,封装员工数据
  $avatar = upload('avatar');
  $data = [
    "name" => $_POST['name'],
    "avatar" => $avatar['data'],
    "mobile" => $_POST['mobile'],
    "sex" => $_POST['sex'],
    "email" => $_POST['email'],
    "address" => $_POST['address'],
    "depid" => $_POST['depid'],
    "jobid" => $_POST['jobid'],
    "createtime" => date('y-m-d H:m:s', time())
  ];
  if ($_POST['jobid'] == 0) {
    ShowMsg("该部门暂无职位，无法添加", "addPerson.php");
  }
  $res = insert("person", $data);
  if ($res) {
    ShowMsg("新增员工成功", "personList.php");
    exit;
  } else {
    ShowMsg("新增员工失败", "personList.php");
    exit;
  }
  
}

// 获取所有部门数据
$depRes = findAll("select * from department");
// 获取所有职位数据
$jobRes = findAll("select * from job");
if (!$depRes) {
  ShowMsg("depRes数据正在紧急修复中...");
}
if (!$jobRes) {
  ShowMsg("jobRes数据正在紧急修复中...");
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
  <!-- 引入 -->
  <?php include_once('./common/meta.php') ?>
</head>

<style>
  .avatar_lab {
    width: 200px;
  }

  .avatar_img {
    width: 200px;
  }
</style>

<body>
  <!--<![endif]-->

  <!-- 引入头部导航 -->
  <?php include_once('./common/navbar.php') ?>

  <!-- 引入侧边菜单栏 -->
  <?php include_once('./common/sidebar.php') ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">添加员工</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">addPerson</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='personList.php'"><i class="icon-list"></i> 返回列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="POST" enctype="multipart/form-data">

                <!-- 员工名称 -->
                <label>员工名称</label>
                <input type="text" class="input-xxlarge" name="name" placeholder="请输入员工名称" required>
                <span class="msg"></span>
                <br>
                <!-- 性别 -->
                <input type="radio" name="sex" id="sex" value="0" checked>保密
                <input type="radio" name="sex" id="sex" value="1">男
                <input type="radio" name="sex" id="sex" value="2">女
                <br>
                <br>
                <!-- 员工电话 -->
                <label>员工电话</label>
                <input type="text" class="input-xxlarge" name="mobile" placeholder="请输入员工电话" required>
                <!-- 员工邮箱 -->
                <label>员工邮箱</label>
                <input type="text" class="input-xxlarge" name="email" placeholder="请输入员工邮箱" required>
                <!-- 员工地址 -->
                <label>员工地址</label>
                <input type="text" class="input-xxlarge" name="address" placeholder="请输入员工地址">
                <!-- 员工部门 -->
                <label>请选择部门</label>
                <select name="depid" id="depid">
                  <option value="0">请选择</option>
                  <?php foreach ($depRes as $item) { ?>
                    <option value="<?php echo $item['id'] ?>"><?php echo $item['name'] ?></option>
                  <?php } ?>
                </select>
                  <!-- 职位 -->
                  <label>请选择职位</label>
                <select name="jobid" id="jobid">
                  <option value="0">请选择</option>
                </select>
                <!-- 员工邮箱 -->
                <!-- 头像 -->
                <label>头像</label>
                <input type="file" name="avatar" id="avatar" hidden>
                <label for="avatar" class="avatar_lab">
                  <img src="./static/images/upload.png" alt="" id="avatar_img">
                </label>

                <button class="btn btn-primary" type="submit" value="提交">提交</button>
              </form>
            </div>
          </div>
        </div>

        <!-- 引入底部 -->
        <?php include_once('./common/footer.php') ?>

      </div>
    </div>
  </div>

</body>

</html>
<!-- 引用公共js文件 -->
<?php include_once('./common/script.php') ?>
<script>
  // 图片预览函数
  function GetObjectURL(file) {
    // createObjectURL  给一个文件对象 可以提取出一个url本地地址出来
    var url = null;

    //判断是否为IE浏览器还是google还是其他浏览器
    if (window.createObjectURL != undefined) {
      url = window.createObjectURL(file)
    } else if (window.URL != undefined) {
      url = window.URL.createObjectURL(file)
    } else if (window.webkitURL != undefined) {
      url = window.webkitURL.createObjectURL(file)
    }

    return url
  }
  $('#avatar').change(function() {
    // 获取选中的文件
    let file = $(this)[0].files[0] ? $(this)[0].files[0] : {}
    // 获取该文件地址
    let src = GetObjectURL(file)
    // console.log(src)
    $('#avatar_img').attr('src', src)
  })

  $("input[name = 'name']").change(function () { 
    let name = $(this).val()
    // console.log(name)
    $.ajax({
      type: "post",
      url: 'addPerson.php',
      data: {
        name,
        action: 'name'
      },
      dataType: 'json',
      success: function(res) {
        if (res.code == 1) {
          $('.msg').text(res.msg).css({"color": "blue"})
        } else {
          $('.msg').text(res.msg).css({"color": "red"})
        }
      }
    })
  })
  $('#depid').change(function() {
    let depid = $(this).val()
    $.ajax({
      type: 'post',
      url: 'addPerson.php',
      data: {
        depid,
        action: 'depid'
      },
      dataType: 'json',
      success: function(res) {
        // console.log(res)
        let option = ''
        if (res.code == 1) {
          res.data.forEach(item => {
            option += `<option value="${item.id}">${item.name}</option>`
          })
        } else {
          option += `<option value=0">请选择</option>`
        }
        $("#jobid").html(option)
      },
      error: function(err) {
        console.log("err")
      }
    })
  })

</script>